package org.paradigmshift.timelapse.ui

import java.awt.Dimension
import scala.swing.Action
import scala.swing.MainFrame
import scala.swing.Menu
import scala.swing.MenuBar
import scala.swing.MenuItem
import scala.swing.SimpleSwingApplication
import javax.swing.JOptionPane
import javax.swing.UIManager
import java.awt.Component
import javax.swing.KeyStroke
import java.awt.event.KeyEvent
import java.awt.event.InputEvent
import java.awt.Toolkit
import java.lang.reflect.Field
import java.util.Properties
import org.paradigmshift.timelapse.TimeLapseApp

object TimeLapseUI {
  val toolkit = Toolkit.getDefaultToolkit
  val icon = toolkit.getImage( getClass.getResource( "/images/clock-128.png" ) )
  val preferencesDialog = new PreferencesDialog
  val loginDialog = new LoginDialog
  val systemMask = toolkit.getMenuShortcutKeyMask
}

class TimeLapseUI extends SimpleSwingApplication {
  
  val searchPanel = new SearchPanel
  
  def top = new MainFrame {
    title = "Time Lapse"
    menuBar = new MenuBar
    contents = searchPanel
    iconImage = TimeLapseUI.icon
    
    // LAF
    if ( OSValidator.isMac ) {
      System.setProperty( "apple.laf.useScreenMenuBar", "true" )
      OSXAdapter.setDockIcon( TimeLapseUI.icon )
      OSXAdapter.setPreferencesHandler( TimeLapseUI.this, TimeLapseUI.this.getClass().getDeclaredMethod( "showPreferences" ) )
      OSXAdapter.setAboutHandler( TimeLapseUI.this, TimeLapseUI.this.getClass().getDeclaredMethod( "showAbout" ) )
      OSXAdapter.setQuitHandler( TimeLapseUI.this, TimeLapseUI.this.getClass().getDeclaredMethod( "quitApp" ) )
    }
    else {
      // Preference/setting menu for other os's
      menuBar.contents += new Menu( "Window" ) {
        contents += new MenuItem( Action( "Preferences" ) {
          def accelerator = KeyStroke.getKeyStroke( KeyEvent.VK_COMMA, TimeLapseUI.systemMask )
          showPreferences
        })
        contents += new MenuItem( Action( "About" ) {
          def accelerator = KeyStroke.getKeyStroke( KeyEvent.VK_A, TimeLapseUI.systemMask )
          showAbout
        })
        contents += new MenuItem( Action( "Quit" ) {
          def accelerator = KeyStroke.getKeyStroke( KeyEvent.VK_Q, TimeLapseUI.systemMask )
          quitApp
        })
      }
    }
    
    val dim = new Dimension( 700, 400 )
    minimumSize = dim
    preferredSize = dim
    size = dim
    centerOnScreen
    pack
  }
  
  def showPreferences = TimeLapseUI.preferencesDialog.open
  def showAbout = JOptionPane.showMessageDialog( null, "Version: " + TimeLapseApp.version, "About", JOptionPane.INFORMATION_MESSAGE )
  def quitApp = this.quit
  
  UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() )
}
